




/*
*************************************************************************
*西瑞克斯研发管理系统
*版 本: 1.0   
*日 期：02/29/2012    
*作 者： 王浩 QQ：873987060   
*************************************************************************   
*/ 
package com.application.yfplacefile.struts.actions;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.manager.commons.config.ConfigFactory;
import com.manager.commons.page.PageBean;
import com.manager.commons.RandomKey;
import com.manager.commons.CommonUtil;
import com.application.attachment.entity.Attachment;
import com.application.attachment.service.AttachmentService;
import com.application.attachment.struts.form.AttachmentForm;
import com.application.mac.struts.form.MacForm;
import com.application.yfplacefile.entity.YfPlacefile;
import com.application.yfplacefile.struts.form.YfPlacefileForm;
import com.application.yfplacefile.service.YfPlacefileService;
import com.application.yfplacefile.commons.Constants;
 /**
 * 
 * Description:YfPlacefileAction<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060    <br />
 * 
 * History: <br />
 * 
 * 02/29/2012 
 * 
 */
public class YfPlacefileAction extends DispatchAction
{
       
       /**
	 * Service层实例
	 */
	private YfPlacefileService yfPlacefileService;

	/**
	 * 日志
	 */
	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * 实体对象
	 */
    private YfPlacefile yfPlacefile;
        
    private AttachmentService attachmentService;


        /**
	 * 采用partialList方式分页程序
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getYfPlacefilePartialList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("searchYfPlacefile" + "开始");
		YfPlacefileForm yfPlacefile = (YfPlacefileForm)form;
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List yfPlacefileList = null;
		resultSize = yfPlacefileService.getAllCountYfPlacefile(yfPlacefile);
		pageBean = PageBean.getPageBean("yfPlacefileTab", resultSize,request);
		yfPlacefileList = yfPlacefileService.searchYfPlacefile(yfPlacefile,pageBean);
		request.setAttribute("yfPlacefileList", yfPlacefileList);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		log.debug("searchYfPlacefile" + "结束");

		return mapping.findForward(Constants.SUCCESS);
	}

        /**
	 * 到达新增页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toAddYfPlacefile(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("toAddYfPlacefiles");
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 增加
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward addYfPlacefile(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("addYfPlacefiles");
		YfPlacefile yfPlacefile = this.getYfPlacefile(form);
         yfPlacefile.setId(RandomKey.generateID_Long_19());
		 yfPlacefile.setOperateid(CommonUtil.getCurrentUser(request).getId());
		 yfPlacefile.setCreateDate(new Date());

		this.yfPlacefileService.insertObject(yfPlacefile);

		return mapping.findForward(Constants.SUCCESS);
	}
        
	/**
	 * 删除
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward deleteYfPlacefile(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("deleteYfPlacefiles");
		YfPlacefileForm yfPlacefileForm = (YfPlacefileForm) form;
		this.yfPlacefileService.deleteYfPlacefiles(yfPlacefileForm);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达更新页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toUpdateYfPlacefile(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("toUpdateYfPlacefiles");

		YfPlacefile yfPlacefile = this.getYfPlacefile(form);
		yfPlacefile = this.yfPlacefileService.getYfPlacefile(yfPlacefile);
		request.setAttribute(Constants.YFPLACEFILE, yfPlacefile);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		return mapping.findForward(Constants.SUCCESS);
	}

        
	/**
	 * 更新
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward updateYfPlacefile(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("updateYfPlacefiles");
		YfPlacefile yfPlacefile = this.getYfPlacefile(form);
	        yfPlacefile.setOperateid(CommonUtil.getCurrentUser(request).getId());
		 yfPlacefile.setCreateDate(new Date());
		this.yfPlacefileService.updateObject(yfPlacefile);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		return mapping.findForward(Constants.SUCCESS);
	}
        
	/**
	 * 查看页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward viewYfPlacefile(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.debug("viewYfPlacefile");
		List<Attachment>  attachList = null;
		YfPlacefileForm yfPlacefileForm = (YfPlacefileForm) form;
		yfPlacefile = this.yfPlacefileService.searchYfPlacefile(yfPlacefileForm, null).get(0);
		
		String projectNo = yfPlacefile.getProjectNo();
		if(StringUtils.isNotBlank(projectNo)){
			AttachmentForm attachmentForm = new AttachmentForm();
			attachmentForm.setBak1(projectNo);
			attachmentForm.setInclude("'10002','10003','10004','10005','10006','10007','10008','10009','30000','30001','30002','30003','30004','30005'");
			attachList = attachmentService.queryAttachment(attachmentForm);
			
		}
		HttpSession session = request.getSession();
		MacForm mac = (MacForm) session.getAttribute("mac");
		String xzQX = "false";
		if(mac!=null && mac.getSpecial().equals("下载特权")){
			xzQX="true";
		}
	
		
		showAttachList(attachList,request);
		 if(attachList==null){
			 attachList = new ArrayList();
		 }
		 yfPlacefile.setFileNum(Long.valueOf(attachList.size()));
		 yfPlacefileService.updateObject(yfPlacefile);
		 request.setAttribute(Constants.YFPLACEFILE, yfPlacefile);
		 request.setAttribute("xzQX", xzQX);
		 request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		return mapping.findForward(Constants.SUCCESS);
	}
	
	private void showAttachList(List<Attachment> attachList,HttpServletRequest request){
		List<Attachment>  lxs = new ArrayList<Attachment> ();
		List<Attachment>  wxht = new ArrayList<Attachment> ();
		List<Attachment>  xmjhs = new ArrayList<Attachment> ();
		List<Attachment>  gysj = new ArrayList<Attachment> ();
		List<Attachment>  xxsj = new ArrayList<Attachment> ();
		List<Attachment>  zs = new ArrayList<Attachment> ();
		List<Attachment>  rjzxt = new ArrayList<Attachment> ();
		List<Attachment>  yjylt = new ArrayList<Attachment> ();
		List<Attachment>  yjpcb = new ArrayList<Attachment> ();
		List<Attachment>  kfzysx = new ArrayList<Attachment> ();
		List<Attachment>  bom = new ArrayList<Attachment> ();
		
		request.setAttribute("lxs", lxs);
		request.setAttribute("wxht", wxht);
		request.setAttribute("xmjhs", xmjhs);
		request.setAttribute("gysj", gysj);
		request.setAttribute("xxsj", xxsj);
		request.setAttribute("zs", zs);
		request.setAttribute("rjzxt", rjzxt);
		request.setAttribute("yjylt", yjylt);
		request.setAttribute("yjpcb", yjpcb);
		request.setAttribute("kfzysx", kfzysx);
		request.setAttribute("bom", bom);
        for (Attachment attachment : attachList) {
			if(attachment.getType().equals(com.manager.commons.Constants.FJ_TYPE_LXSQ)){   //立项申请
				lxs.add(attachment);
			}
			if(attachment.getType().equals(com.manager.commons.Constants.FJ_TYPE_WXHT)){   //外協合同
				wxht.add(attachment);
			}
			if(attachment.getType().equals(com.manager.commons.Constants.FJ_TYPE_XMJH)){   //项目计划
				xmjhs.add(attachment);
			}
			if(attachment.getType().equals(com.manager.commons.Constants.FJ_TYPE_GYSJ)){   //概要设计
				gysj.add(attachment);
			}
			if(attachment.getType().equals(com.manager.commons.Constants.FJ_TYPE_YJXXSJ) || attachment.getType().equals(com.manager.commons.Constants.FJ_TYPE_RJXXSJ)
					|| attachment.getType().equals(com.manager.commons.Constants.FJ_TYPE_JKXXSJ) || attachment.getType().equals(com.manager.commons.Constants.FJ_TYPE_XXSJ)){   //详细设计
				xxsj.add(attachment);
			}
			if(attachment.getType().equals(com.manager.commons.Constants.FJ_TYPE_PILOT)){   //中试
				zs.add(attachment);
			}
			if(attachment.getType().equals(com.manager.commons.Constants.FJ_TYPE_RJZXTDM)){   //软件子系统代码
				rjzxt.add(attachment);
			}
			if(attachment.getType().equals(com.manager.commons.Constants.FJ_TYPE_YJYLT)){   //硬件原理图
				yjylt.add(attachment);
			}
			if(attachment.getType().equals(com.manager.commons.Constants.FJ_TYPE_PCB)){   //硬件PCB
				yjpcb.add(attachment);
			}
			if(attachment.getType().equals(com.manager.commons.Constants.FJ_TYPE_KFZYSX)){   //开发注意事项
				kfzysx.add(attachment);
			}
			if(attachment.getType().equals(com.manager.commons.Constants.FJ_TYPE_BOM)){   //BOM清单
				bom.add(attachment);
			}
		}
		
		
		
	}
	

	// get--set
	/**
	 * @return the YfPlacefileService
	 */
	public YfPlacefileService getYfPlacefileService() {
		return this.yfPlacefileService;
	}

	/**
	 * @param YfPlacefileService
	 *  the YfPlacefileService to set
	 */
	public void setYfPlacefileService(YfPlacefileService yfPlacefileService) {
		this.yfPlacefileService = yfPlacefileService;
	}

	/**
	 * 获取YfPlacefile实体
	 * @param ActionForm
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @return the YfPlacefile
	 */
	public YfPlacefile getYfPlacefile(ActionForm form) throws IllegalAccessException, InvocationTargetException {
		YfPlacefileForm yfPlacefileForm = (YfPlacefileForm) form;
		yfPlacefile = new YfPlacefile();
		BeanUtils.copyProperties(yfPlacefile, yfPlacefileForm);
		return yfPlacefile;
	}
    
        public void setYfPlacefile(YfPlacefile yfPlacefile) {
		this.yfPlacefile = yfPlacefile;
	}

		public AttachmentService getAttachmentService() {
			return attachmentService;
		}

		public void setAttachmentService(AttachmentService attachmentService) {
			this.attachmentService = attachmentService;
		}

}
